/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include <string.h>
#include <stdlib.h>
#include "usart.h"
#include "gpio.h"

void SystemClock_Config(void);

uint32_t ticks_now, ticks_recv_ticks;
char gUartBuf[512], recvBuf[512];
int gRecvUartLen = 0;

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if (gRecvUartLen < sizeof(gUartBuf)-1){
		gUartBuf[gRecvUartLen + 1] = '\0';
		gRecvUartLen++;

		//记录下最近一个命令的时间
		ticks_recv_ticks = ticks_now; //记录下最近的收到的时间
		HAL_UART_Receive_IT(&huart4,  (unsigned char *)&(gUartBuf[gRecvUartLen]), 1);
	}
}

//去掉后面的空格，换行符之类的
void trim(char* text){
	int len = strlen(text);
	int i;
	for(i=len-1; i>=0; i--){
		if ((text[i] == '\r')
			||(text[i] == '\n')
			||(text[i] == '\t')
			||(text[i] == ' ')){
			text[i]='\0';
		} else{
			return;
		}
	}
}

int main(void)
{
	HAL_Init();

	if(IS_ENGINEERING_BOOT_MODE())
	{
		/* Configure the system clock */
		SystemClock_Config();
	}

	MX_GPIO_Init();
	MX_UART4_Init();

	ticks_recv_ticks = 0;
	gRecvUartLen = 0;
	memset(gUartBuf, 0, sizeof(gUartBuf));
	HAL_UART_Receive_IT(&huart4,  (unsigned char *)gUartBuf, 1);

	printf("applicaton start...\r\n");
	while (1)
	{
		/* USER CODE END WHILE */
		ticks_now = HAL_GetTick();

		//有数据，而且数据间隔大于50ms，就认为是一个命令
		if ((gRecvUartLen > 0) && (ticks_now - ticks_recv_ticks > 50)){
			int len;

			//禁止中断
			HAL_UART_AbortReceive(&huart4);

			//禁止串口中断
			memcpy(recvBuf, gUartBuf, gRecvUartLen);
			len = gRecvUartLen;
			recvBuf[len]='\0';
			gRecvUartLen = 0;
			//重新开始接受
			HAL_UART_Receive_IT(&huart4,  (unsigned char *)gUartBuf, 1);

			//打开中断，继续接受
			trim((char *)recvBuf);

			printf("rsp>>: %s\r\n", recvBuf);
		}
	}
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.HSIDivValue = RCC_HSI_DIV1;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.PLL2.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.PLL3.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.PLL4.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** RCC Clock Config
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_ACLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
                              |RCC_CLOCKTYPE_PCLK3|RCC_CLOCKTYPE_PCLK4
                              |RCC_CLOCKTYPE_PCLK5;
  RCC_ClkInitStruct.AXISSInit.AXI_Clock = RCC_AXISSOURCE_HSI;
  RCC_ClkInitStruct.AXISSInit.AXI_Div = RCC_AXI_DIV1;
  RCC_ClkInitStruct.MCUInit.MCU_Clock = RCC_MCUSSOURCE_HSI;
  RCC_ClkInitStruct.MCUInit.MCU_Div = RCC_MCU_DIV1;
  RCC_ClkInitStruct.APB4_Div = RCC_APB4_DIV1;
  RCC_ClkInitStruct.APB5_Div = RCC_APB5_DIV1;
  RCC_ClkInitStruct.APB1_Div = RCC_APB1_DIV1;
  RCC_ClkInitStruct.APB2_Div = RCC_APB2_DIV1;
  RCC_ClkInitStruct.APB3_Div = RCC_APB3_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

